package com.yegs.jindie.quartz;

import com.yegs.jindie.base.QuartzManager;
import com.yegs.jindie.entity.JobVo;
import com.yegs.jindie.serviceImpl.JobServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class StartJob implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private JobServiceImpl jobService;

    public void run(){
        System.out.println("启动......");
//        QuartzManager.addJob(
//                "SpecialPeriodJob",
//                "SpecialPeriodJobGroup",
//                "SpecialPeriodTrigger",
//                "SpecialPeriodTriggerGroup",
//                ButtonTimerJob.class,
//                "0/30 * * * * ?");
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("启动定时任务......");
        //run();
       getJobs1();
    }

    private void getJobs1(){
        List<JobVo> JobVos = jobService.findAll();
        for(JobVo job : JobVos){
            if(job.getStates().equals("true")){
                QuartzManager.addJob(job);
            }
        }
    }

    private static void getJobs(){
        List<JobVo> jobs = new ArrayList<>();
        JobVo deliveryJob = new JobVo("DeliveryJob","com.yegs.jindie.quartz.DeliveryJob", "5 * * * * ?");
        JobVo inventoryJob = new JobVo("InventoryJob","com.yegs.jindie.quartz.InventoryJob", "15 * * * * ?");
        JobVo purchaseJob = new JobVo("PurchaseJob","com.yegs.jindie.quartz.PurchaseJob", "0 * * * * ?");
        QuartzManager.addJob(deliveryJob);
        QuartzManager.addJob(inventoryJob);
        QuartzManager.addJob(purchaseJob);
    }
}